
var silks = { // 全局
	loads: function(arr, cbk) { // 加载js文件
		var scrall = document.scripts,
			nums = 0,
			sto = setInterval(function() {
				if(nums == arr.length) {
					clearInterval(sto);
					if(typeof cbk === "function") {
						cbk();
					}
				}
			}, 10);

		function scr(url, cbks) {
			var scrs = document.createElement("script");
			scrs.src = url;
			scrs.onload = cbks;
			scrs.async = false;
			document.body.appendChild(scrs);
		}
		for(var i = 0; i < arr.length; i++) {
			for(var j = 0; j < scrall.length; j++) {
				if(new RegExp(arr[i]).test(scrall[j].src)) {
					document.body.removeChild(scrall[j])
				}
			}
			scr(arr[i], function() {
				nums++;
			})
		}
	},
	loaded: function(boo){ // 加载GIF图
		boo?$("body").prepend('<img class="loads" src="../../src/images/loading.gif">'):$(".loads").remove();
	},
	temp: function(select, url, data, callback) { // 模板渲染

		if(data.flags !== false) { // 默认添加  flags为false则不添加
			data.AppID = AppID;
			data.Token = Token;
		}

		$.get(urls + url, data, function(res) { // 固定get请求 
			if(res.Return_ID == 0) {

				var res = res.Return_Data.length === undefined ? res.Return_Data : res // 判断数组 
				res.img = imgurlbig; // 添加图片总路径

				laytpl($(select[0]).html()).render(res, function(html) {
					$(select[1]).html(html);
				});

			}

			callback && callback.call(res);

		});
	},
	flows: function(select, url, data, callback) { // 信息流下拉加载
		if(data.flags !== false) { // 默认添加  flags为false则不添加
			data.AppID = AppID;
			data.Token = Token;
		}
		layui.use('flow', function() {
			var flow = layui.flow;
			flow.load({
				elem: select[1],
				done: function(page, next) {
					data.PageIndex = page;
					$.post(urls + url, data, function(res) {
						if(res.Return_ID == 0) {
							res.Return_Data.img = imgurlbig;
							laytpl($(select[0]).html()).render(res.Return_Data, function(html) {
								$("" + select[1] + " div.layui-flow-more").before(html);
							});

							flow.lazyimg();
							next('', page < res.Return_Data.PageCount);
							if(callback !== undefined) {
								callback.call(res);
							}
						}
					});
				}
			});
		});
	},
	setAssign: function(dataArr, className) { // 添加数据渲染
		var classlen = $(className).length;
		for(var i = 0; i < classlen; i++) {
			$(className).eq(i).html(dataArr[i]);
		}
	},
	arr_str: function(num, attrs) { // 数组转换成字符串
		var result = [];
		for(var i = 0; i < num.length; i++) {
			result.push(num[i][attrs]);
		}
		return result.join(" ");
	},
	fooChoo: function() { // 监听商城选择
		var that = this;
		$("footer i:eq(" + that.getByVal("type") + "),footer p:eq(" + that.getByVal("type") + ")").addClass("huef36"); // 当前页颜色标记

		$("#slicksShop").on("click", function() {
			if($(this).attr("index") == 0) {
				$("div.sc-box,.switch:first").show();
				$(".switch:last").hide();
				$(this).attr("index", "1")
			} else {
				$("div.sc-box,.switch:first").hide();
				$(".switch:last").show();
				$(this).attr("index", "0")
			}

		});
	},
	goodsNum: function(funs) { // 执行加减
		 var focu = undefined;
		$(".amount-sup").click(function () {
			var $num = $(this).siblings(".amount-num");
			$num.val( Number($num.val())+1);
			funs&&funs.call(this,$num.val());
		});
		$(".amount-sub").click(function () {
			var $num = $(this).siblings(".amount-num");
			$num.val()>1&&$num.val( Number($num.val())-1)&&funs&&funs.call(this,$num.val());
			 
		});
		$(".amount-num").focus(function () {
			focu = this.value;
		});
		$(".amount-num").blur(function () {
			this.value = this.value?this.value : 1; //失去焦点为空赋值为1
			this.value = /\d+/.test(this.value)?this.value : 1; //失去焦点为非数字赋值为1
			 console.log()
			focu!=this.value&&funs&&funs.call(this,this.value); // 获得焦点和失去焦点的值相等则不执行请求
		});
		
	},
	getByVal: function(name) { // 获取路传值
		var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
		var r = window.location.search.substr(1).match(reg);
		if(r != null) {
			return decodeURIComponent(r[2]);
		}
		return null;
	},
	checkMark: function() { // 点击复选框

		document.addEventListener("click", function(e) {
			if(e.target.type === "checkbox") {
				var $ed = $("#edit").attr("index");
				var $ts = $(e.target),
					eta = e.target,
					$nm = $ts.attr("name"),
					$di = $ts.attr("data-i"),
					$dj = $ts.attr("data-j"),
					$oip = $("input[name='oip']"),
					$iip = $("input[name='iip']"),
					$aip = $("input[name='aip']"),
					$s1 = $di ? $oip.eq($di) : $di,
					$s2 = $di ? $("input[data-a=" + $di + "]") : $di,
					imp = function(a, b) {
						a.each(function() {
							this.checked = b;
							this.className = b ? "dress-dio dress-check" : "dress-dio";
						});
					},
					flag = function(a, b) {
						a.className = b ? "dress-dio dress-check" : "dress-dio";
					},
					ca = function(a, b) {
						a.get(0).checked = b;
						a.get(0).className = b ? "dress-dio dress-check" : "dress-dio";
					},
					al = function(a) {
						var m = 0;
						a.each(function() {
							this.checked && m++;
						});
						return m === a.length;
					},
					nums = function() {
						var allPa = 0; // 总现金
						var allPi = 0; // 总积分

						$iip.each(function() {
							if(this.checked) {
								var si = this.getAttribute("data-i"),
									sj = this.getAttribute("data-j"),
									mn = edit.Return_Data[si].CarProduct[sj].Car_Amount, // 数量
									pa = edit.Return_Data[si].CarProduct[sj].ProductSku_PriceCash, // 单价
									pi = edit.Return_Data[si].CarProduct[sj].ProductSku_PricePoints; // 积分
								allPa = allPa + Number(mn) * Number(pa);
								allPi = allPi + Number(mn) * Number(pi);

							}
						});
						return {
							allPa: allPa,
							allPi: allPi
						}
					};

				switch($nm) {
					case "oip":
						if(eta.checked) {
							imp($s2, true);
							flag(eta, true);
							al($iip) && (ca($aip, true));

						} else {
							imp($s2, false);
							flag(eta, false);
							ca($aip, false);
						}
						break;
					case "iip":

						if(eta.checked) {
							flag(eta, true);
							al($s2) && (ca($s1, true));
							al($iip) && (ca($aip, true));
						} else {
							ca($s1, false);
							ca($aip, false);
							flag(eta, false);
						}
						break;
					case "aip":

						if(eta.checked) {
							imp($oip, true);
							imp($iip, true);
							flag(eta, true);

						} else {
							imp($oip, false);
							imp($iip, false);
							flag(eta, false);
						}
						break;
					default:
						/*没有操作*/
						break;
				}
				
				// 显示总价格
				let allnums = null;
				!Number($ed) && (allnums = nums(), $("#carcash").html(allnums.allPa), $("#carpoint").html(allnums.allPi));

			}
		});

	}

};
var layers = function(txt, callback) {
	layer.open({
		content: txt,
		skin: 'msg',
		time: 2,
		end: callback
	});
};
var getAjaxs = function(type, url, data, before, callback) {
	$.ajax({
		type: type,
		url: urls + url,
		data: data,
		beforeSend: before,
		success: callback
	});
};